
package todolist.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * General utilities. All methods here are static.
 * @author Ivan
 */
public class Utils {
	
	/**
	 * Determine line-separator, independent of OS used, typically backslash n.
	 */
	public static String getLineSeparator() {
		return System.getProperty("line.separator");
	}
	
	/**
	 * Returns resulting String after removing N words from Input, or empty String if too many words removed.
	 * Input must not be null, and N has to be positive.
	 * Word is defined by one or more non-whitespace characters.
	 */
	public static String discardNWordsFromString(String pstrInput, int pintN) {
		// parameter validation
		if (pstrInput == null)
			throw new IllegalArgumentException("Utils.discardNWordsFromString(): Input must not be null.");
		if (pintN < 0)
			throw new IllegalArgumentException("Utils.discardNWordsFromString(): Number of words to discard must be positive.");
		String strWords = "\\s*";
		while (pintN-- > 0)
			strWords += "(?:\\S+\\s+)";
		Pattern objPattern = Pattern.compile(strWords + "(.*)", Pattern.CASE_INSENSITIVE);
		Matcher objMatcher = objPattern.matcher(pstrInput);
		if (!objMatcher.matches())
			return "";
		return objMatcher.group(1);
	}
	
}
